package com.sky.codeGenerate.config.exception;

import com.sky.common.basic.Results;
import com.sky.common.enums.SysEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
public class ApiGlobalException {

    private int status = 500;

    public ApiGlobalException() {
    }

    public ApiGlobalException(int status) {
        this.status = status;
    }

    @ExceptionHandler(Exception.class)
    public Results errorHandler(HttpServletRequest req, Exception e, HttpServletResponse response) {
        log.error("errorHandler全局异常:{}", req.getServletPath(), e);
        String retStatus = "500";
        response.setStatus(status);
        String servletPath = req.getServletPath();
        Results rs = new Results(SysEnum.ERROR500);
        rs.setMsg(servletPath + ":" + e.getMessage());
        if (e instanceof NoHandlerFoundException) {
            retStatus = "400";
            rs.fail(SysEnum.ERROR404.getCode(), servletPath + " not found.");
        }
        if (e instanceof BIZException) {
            String[] strings = e.getMessage().split(BIZException.SPLIT);
            if (strings.length >= 2) {
                Integer value = Integer.valueOf(strings[0]);
                if (value.equals(SysEnum.ERROR500.getCode())) {
                    retStatus = "500";
                } else {
                    retStatus = "400";
                }
                rs.setCode(value);
                rs.setMsg(strings[1]);
            }
        }
        if (e instanceof MethodArgumentNotValidException) {
            retStatus = "400";
            MethodArgumentNotValidException methodArgumentNotValidException = ((MethodArgumentNotValidException) e);
            String defaultMsg = "";
            for (FieldError error : methodArgumentNotValidException.getBindingResult().getFieldErrors()) {
                defaultMsg = error.getDefaultMessage();
            }
            rs.setCode(SysEnum.ERROR1002.getCode());
            rs.setMsg(defaultMsg);
        } else if (e instanceof BindException) {
            retStatus = "400";
            BindException bindException = (BindException) e;
            List<ObjectError> allErrors = bindException.getAllErrors();
            String defaultMsg = "";
            for (ObjectError objectError : allErrors) {
                defaultMsg = objectError.getDefaultMessage();
                break;
            }
            rs.setCode(SysEnum.ERROR1002.getCode());
            rs.setMsg(defaultMsg);
        }
        response.addHeader("Ret-Status", retStatus);
        return rs;
    }
}
